//
//  SecondViewController.m
//  UI_09TabBarController
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 XWH. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.title = @"第二页";
    [self creatScroller];
    // Do any additional setup after loading the view.
}

- (void)creatScroller{
    UIScrollView  *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    scrollView.backgroundColor = [UIColor yellowColor];
    scrollView.tag = 1001;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 5, 0);
    
    scrollView.pagingEnabled = YES;

    scrollView.showsHorizontalScrollIndicator = YES;

    scrollView.bounces = NO;

    scrollView.contentOffset = CGPointMake(scrollView.frame.size.width , 0);
    

    scrollView.delegate = self;

    
    [self.view addSubview:scrollView];

    for (int i = 0 ; i < 5 ; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
     
        UIScrollView *little = [[UIScrollView alloc] initWithFrame:CGRectMake(scrollView.frame.size.width * (i), 0, scrollView.frame.size.width, scrollView.frame.size.height)];
        
       
        little.maximumZoomScale = 2;
        little.minimumZoomScale = 0.5;
        
        little.delegate = self;
        
        [little addSubview:imageView];
        [scrollView addSubview:little];
        
    }
    
    [scrollView release];
    
    
    UIPageControl * pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 625, self.view.frame.size.width , 20)];
    
    pageC.tag = 1002;
    
    pageC.numberOfPages = 3;
    
   
    [pageC addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:pageC];
    
    [pageC release];
    

    
}

- (void)pageControlAction:(UIPageControl *)pageC {
   
    UIScrollView * scrollView = [self.view viewWithTag:1001];
    
    [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width * pageC.currentPage, 0) animated:YES];
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
 
    UIPageControl *pageControl = [self.view viewWithTag:1002];
 
    pageControl.currentPage = scrollView.contentOffset.x/scrollView.frame.size.width - 1;
    if (pageControl.currentPage == 0) {
        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width * 3, 0) animated:NO];
        pageControl.currentPage = 2;
    } else if (scrollView.contentOffset.x == 4 * scrollView.frame.size.width){
        [scrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0) animated:NO];
        pageControl.currentPage = 0;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
